package com.jshoperxms.controller.mall.backstage.init;

import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.redis.RedisBaseOperation;
import com.jshoperxms.redis.RedisBaseTDao;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;

import com.baidu.ueditor.util.QQiNiuUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jshoperxms.controller.utils.statickey.RedisKey;
import com.jshoperxms.entity.GlobalParamM;
import com.jshoperxms.service.GlobalParamService;

/**
 * 启动加载配置项目
 * 
 * @author sdywcd
 *
 */
public class InitDataListenter implements InitializingBean, ServletContextAware {
	private static final Logger log = LoggerFactory
			.getLogger(InitDataListenter.class);
	@Resource
	private GlobalParamService globalParamService;
	@Resource
	private RedisBaseTDao<String> redisBaseTDao;


	@Override
	public void setServletContext(ServletContext servletContext) {
		Criterion criterion = Restrictions.eq("gkey", RedisKey.CONFIG);
		GlobalParamM globalParamM = this.globalParamService.findOneByCriteria(
				GlobalParamM.class, criterion);
		if (globalParamM != null) {
			Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
					.create();
			Map<String, String> configs = gson.fromJson(
					globalParamM.getGvalue(),
					new TypeToken<Map<String, String>>() {
					}.getType());

			this.redisBaseTDao.put(RedisKey.CONFIG,RedisKey.CONFIG, GsonJson.parseDataToJson(configs));
			for (Iterator i = configs.keySet().iterator(); i.hasNext();) {
				Object obj = i.next();
				System.out.println("key=" + obj + " value=" + configs.get(obj));
			}
		}


		
	}



	@Override
	public void afterPropertiesSet() throws Exception {
		QQiNiuUtil.genUptoken();
	}

}
